package com.ztx.controller;

import com.ztx.annotation.AuthToken;
import com.ztx.pojo.Permission;
import com.ztx.service.PermissionService;
import com.ztx.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@AuthToken
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    PermissionService permissionService;

    @GetMapping("/list")
    public CommonResult<List<Permission>> list(){
        return CommonResult.success(permissionService.list());
    }

    @DeleteMapping("/{permissionId}")
    public CommonResult<String> deleteById(@PathVariable Long permissionId){

        permissionService.removeById(permissionId);

        return CommonResult.success("success");
    }

    @PostMapping("/addPermission")
    public CommonResult<String> addPermission(@RequestBody Permission permission){
        permissionService.save(permission);
        return CommonResult.success("success");
    }

    @GetMapping("/{permissionId}")
    public CommonResult<Permission> getByPermissionId(@PathVariable Long permissionId){

        Permission permission = permissionService.getById(permissionId);

        return CommonResult.success(permission);
    }

    @PutMapping("updatePermission")
    public CommonResult<String> updatePermission(@RequestBody Permission permission){
        permissionService.updateById(permission);
        return CommonResult.success("success");
    }
}
